package org.querycreator.utils;

import java.io.FileInputStream;
import java.net.URL;
import java.sql.Connection;

import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.hibernate.Session;
import org.junit.Ignore;

/**
 * 
 * @author Luciano Molinari
 *
 */
@Ignore
public class DBUnitUtils {

	@SuppressWarnings("deprecation")
	public static void loadDataDbUnit(String file, Character op) {
		Session session = ConnectionUtils.getSession();
		Connection conn = session.connection();
		IDatabaseConnection iDB = new DatabaseConnection(conn);
				
		try {
			URL url = DBUnitUtils.class.getResource(file);
			FileInputStream streamFile = new FileInputStream(url.getFile());
			
			if(op.equals('I')) {
				DatabaseOperation.CLEAN_INSERT.execute(iDB, new FlatXmlDataSet(streamFile));
			} else if(op.equals('D')) {
				DatabaseOperation.DELETE.execute(iDB, new FlatXmlDataSet(streamFile));
			}
		} catch(Exception e) {
			e.printStackTrace();
		}				
		session.close();
	}
	
}